﻿#include "passiveequipment.h"

struct PassiveEquipmentPrivate
{
    PassiveEquipment* _this;

    PassiveEquipmentPrivate(PassiveEquipment* p);

    SecsEnum::ConnectionState currentConnectionState;

    SecsEnum::CommunicationState currentCommunicationState;

    SecsEnum::ControlState currentControlState;

    QTcpSocket* socket = new QTcpSocket();

    QTcpServer* server = new QTcpServer();

    /*
     * 该条消息的目的仅仅是为了测试
     * 主要格式为
     * <L 2>
     *  <L 1>
     *      <Boolean true>
     *  <L 2>
     *      <ASCII "ABC">
     *      <U4 1 2 3>
     * <L>
     *
     *
     * 该项目由于stream和function的缘故并没有完成
     * 我们在socket的connect函数中,无论接受到什么消息统一回复上述的MessageText
     */
    TextList* message = new TextList();
};

PassiveEquipmentPrivate::PassiveEquipmentPrivate(PassiveEquipment *p)
{
    _this = p;

    TextList* l1 = new TextList();
    l1->addItem(new TextBoolean(true));

    TextList* l2 = new TextList();
    l2->addItem(new TextASCII("ABC"));
    l2->addItem(new TextU4({1, 2, 3}));

    message->addItem(l1);
    message->addItem(l2);
}

PassiveEquipment::PassiveEquipment()
{
    d = std::make_shared<PassiveEquipmentPrivate>(this);

    d->currentConnectionState
        = SecsEnum::NOT_CONNECTED;

    d->currentCommunicationState
        = SecsEnum::NOT_ENABLED;

    d->currentControlState
        = SecsEnum::EQUIPMENT_OFFLINE;

    connect(d->server, &QTcpServer::newConnection, this, [=](){
        d->socket = d->server->nextPendingConnection();
        emit SendMsg(tr("远程主机和当前设备建立了TCP连接"));

        /*
         * 当有新的TCP连接发送过来时,更新当前设备的ConnectionState和CommunicationState
         * 同时进行socket对象的连接槽绑定
         */
        d->currentConnectionState
            = SecsEnum::NOT_SELECTED;
        d->currentCommunicationState
            = SecsEnum::NOT_COMMUNICATING;
        emit ConnectionStateChange(SecsEnum::NOT_SELECTED);
        emit CommunicationStateChange(SecsEnum::NOT_COMMUNICATING);

        connect(d->socket, &QTcpSocket::disconnected, this, [=](){
            emit SendMsg(tr("远程主机和当前设备断开了TCP连接"));

            d->currentConnectionState
                = SecsEnum::NOT_CONNECTED;
            d->currentCommunicationState
                = SecsEnum::NOT_ENABLED;
            emit ConnectionStateChange(SecsEnum::NOT_CONNECTED);
            emit CommunicationStateChange(SecsEnum::NOT_ENABLED);
        });


        /*
         * 当socket读取到消息后,就需要针对发送来的消息进行逐一的判断
         * 需要注意的是:
         * 倘若当前接受到的消息使得设备端的XXXState发生变化时,就需要在完成处理后进行return操作
         * 不进行return操作,就有可能会进入后续的if判断中
         */
        connect(d->socket, &QTcpSocket::readyRead, this, [=](){
            QByteArray message = d->socket->readAll();


            /*
             * 针对远程主机发送过来的DataMessage,无论是何种消息,均需要放在以下七种情况中讨论,虽然冗余,但是最为保险
             * condition1:NOT_CONNECTED  -->TCP都没有建立,最不可能出现的情况
             * condition2:NOT_SELECTED  -->SECS通信没有建立,最不可能出现的情况
             * condition3:SELECTED && NOT_COMMUNICATING && 接收到ReplyMessage=S1F14  -->通信链路建立,CommunicatingState变为COMMUNICATING
             * condition4:SELECTED && NOT_COMMUNICATING && 接收到RimaryMessage=S1F13  -->通信链路建立,CommunicationState变为COMMUNICATING
             * condition5:SELECTED && NOT_COMMUNICATING && 接收到ReplyMessage=!S1F14  -->通信链路未建立,拒绝该消息
             * condition6:SELECTED && NOT_COMMUNICATING && 接收到RimaryMessage=!S1F13  -->通信链路未建立,拒绝该消息
             * condition7:COMMUNICATING  -->发送SxFy+1,并且数据载荷使用提前准备好的d->message
             */
            if(ParseUtil::Is_DataMessage(message))
            {
                /*
                 * 第一种情况:接收到DataMessage,并且为PrimaryMessage
                 * 说明该消息是Host主动发送过来的,我们需要给予一个ReplyMessage
                 */
                if(ParseUtil::Is_PrimaryMessage(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接都没有建立,逗我玩呢?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("SelectProcedure没有进行,如何传输DataMessage?"));
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED
                        && d->currentCommunicationState == SecsEnum::NOT_COMMUNICATING)
                    {
                        if(Is_S1F13(message))
                        {
                            emit SendMsg(tr("当前设备的状态是NOT_COMMUNICATING,并且您接收到的消息是S1F13"));
                            quint16 sessionId = ParseUtil::Get_SessionId(message);
                            quint32 transactionId = ParseUtil::Get_TransactionId(message);
                            quint8 stream = ParseUtil::Get_Stream(message);
                            quint8 function = ParseUtil::Get_Function(message);
                            Text* data = d->message;
                            QByteArray datamessage_reply = MessageUtil::SxFy(sessionId, transactionId, stream, function + 1, false, data);
                            d->socket->write(datamessage_reply);

                            emit SendMsg(tr("收到主机发送的S1F13消息,他的数据格式如下所示:"));
                            if(ParseUtil::Get_MessageText(message) == nullptr)
                            {
                                emit SendMsg(tr("S1F13中不包含数据"));
                            }
                            else
                            {
                                emit SendMsg(ParseUtil::Get_MessageText(message)->getString(0));
                            }

                            emit SendMsg(tr("给予主机一个S1F14消息,他的数据格式如下所示:"));
                            if(data->getLength() == 0)
                            {
                                emit SendMsg(tr("S1F14中不包含数据"));
                            }
                            else
                            {
                                emit SendMsg(data->getString(0));
                            }

                            d->currentCommunicationState = SecsEnum::COMMUNICATING;
                            emit CommunicationStateChange(SecsEnum::COMMUNICATING);
                            return;
                        }

                        if(!Is_S1F13(message))
                        {
                            emit SendMsg(tr("当前设备的状态是NOT_COMMUNICATING,并且您接收到的消息不是S1F13"));
                            emit SendMsg(tr("该条消息不予回复,请提示主机发送S1F13建立通信链路"));
                        }
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED
                        && d->currentCommunicationState == SecsEnum::COMMUNICATING)
                    {
                        quint16 sessionId = ParseUtil::Get_SessionId(message);
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        quint8 stream = ParseUtil::Get_Stream(message);
                        quint8 function = ParseUtil::Get_Function(message);
                        Text* data = d->message;
                        QByteArray datamessage_reply = MessageUtil::SxFy(sessionId, transactionId, stream, function + 1, false, data);
                        d->socket->write(datamessage_reply);

                        emit SendMsg(tr("收到主机发送的Primary消息S%1F%2,他的数据格式如下所示:").arg(stream).arg(function));
                        if(ParseUtil::Get_MessageText(message) == nullptr)
                        {
                            emit SendMsg(tr("PrimaryMessage中不包含数据"));
                        }
                        else
                        {
                            emit SendMsg(ParseUtil::Get_MessageText(message)->getString(0));
                        }

                        emit SendMsg(tr("给予主机一个Reply消息S%1F%2,他的数据格式如下所示:").arg(stream).arg(function + 1));
                        if(data->getLength() == 0)
                        {
                            emit SendMsg(tr("ReplyMessage中不包含数据"));
                        }
                        else
                        {
                            emit SendMsg(data->getString(0));
                        }
                    }
                }


                /*
                 * 第二种情况:接收到DataMessage,并且为ReplyMessage
                 * 说明该消息是我们之前发送给Host的PrimaryMessage的反馈,我们只负责读取即可
                 */
                if(ParseUtil::Is_ReplyMessage(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接都没有建立,逗我玩呢?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("SelectProcedure没有进行,如何传输DataMessage?"));
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED
                        && d->currentCommunicationState == SecsEnum::NOT_COMMUNICATING)
                    {
                        if(Is_S1F14(message))
                        {
                            emit SendMsg("当前设备的状态是NOT_COMMUNICATING,并且您接收到的消息是S1F14");
                            emit SendMsg(tr("收到主机发送的S1F14消息,他的数据格式如下所示:"));
                            if(ParseUtil::Get_MessageText(message) == nullptr)
                            {
                                emit SendMsg(tr("S1F14中不包含数据"));
                            }
                            else
                            {
                                emit SendMsg(ParseUtil::Get_MessageText(message)->getString(0));
                            }

                            d->currentCommunicationState = SecsEnum::COMMUNICATING;
                            emit CommunicationStateChange(SecsEnum::COMMUNICATING);
                            return;
                        }

                        if(!Is_S1F14(message))
                        {
                            emit SendMsg(tr("该条消息作废,设备尚处于NOT_COMMUNICATING,只能够接受的S1F14类型的DataMessage"));
                        }
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED
                        && d->currentCommunicationState == SecsEnum::COMMUNICATING)
                    {
                        emit SendMsg(tr("收到主机发送的Reply消息,他的数据格式如下所示:"));
                        if(ParseUtil::Get_MessageText(message) == nullptr)
                        {
                            emit SendMsg(tr("ReplyMessage中不包含数据"));
                        }
                        else
                        {
                            emit SendMsg(ParseUtil::Get_MessageText(message)->getString(0));
                        }
                    }
                }
            }

            /*
             * 针对远程主机发送过来的ControlMessage,无论是何种消息,均需要放在以下三种情况中讨论,虽然冗余,但是最为保险
             * condition1:NOT_CONNECTED
             * condition2:NOT_SELECTED
             * condition3:SELECTED
             */
            if(ParseUtil::Is_ControlMessage(message))
            {
                /*
                 * 第三种情况:接收到来自主机Host的Select请求
                 * 我们需要给予一个Select的回应
                 */
                if(ParseUtil::Is_SelectReq(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接没有建立,这个消息你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的SelectReq,且当前设备处于NOT_SELECTED"));
                        emit SendMsg(tr("设备回复了SelectRsp,且设备的ConnectionState从NOT_SELECTED-->SELECTED"));
                        quint16 sessionId = ParseUtil::Get_SessionId(message);
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        SecsEnum::SelectStatus select_status = SecsEnum::CommunicationEstablished;
                        QByteArray message = MessageUtil::Select_Rsp(sessionId, transactionId, select_status);
                        d->socket->write(message);

                        d->currentConnectionState = SecsEnum::SELECTED;
                        emit ConnectionStateChange(SecsEnum::SELECTED);
                        return;
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的SelectReq,且当前设备处于SELECTED"));
                        emit SendMsg(tr("设备回复了SelectRsp,但是不会发生任何变化"));
                        quint16 sessionId = ParseUtil::Get_SessionId(message);
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        SecsEnum::SelectStatus select_status = SecsEnum::CommunicationAlreadyActive;
                        QByteArray message = MessageUtil::Select_Rsp(sessionId, transactionId, select_status);
                        d->socket->write(message);
                    }
                }

                /*
                 * 第四种情况:接收到来自主机Host的Select回应
                 * 说明在此之前,设备端自己主动尝试向主机发送Select请求
                 */
                if(ParseUtil::Is_SelectRsp(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接没有建立,这个消息你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的SelectRsp,且当前设备处于NOT_SELECTED"));
                        emit SendMsg(tr("设备之前发送的SelectReq成功响应,ConnectionState从NOT_SELECTED-->SELECTED"));

                        d->currentConnectionState = SecsEnum::SELECTED;
                        emit ConnectionStateChange(SecsEnum::SELECTED);
                        return;
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的SelectRsp,且当前设备处于SELECTED"));
                        emit SendMsg(tr("设备之前发送的SelectReq成功响应,但是不会发生任何变化"));
                    }
                }

                /*
                 * 第五种情况:接收到来自主机Host的DeSelect请求
                 * 我们需要给予一个DeSelect的回应
                 */
                if(ParseUtil::Is_DeselectReq(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接没有建立,这个消息你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        quint16 sessionId = ParseUtil::Get_SessionId(message);
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        SecsEnum::DeselectStatus deselect_status = SecsEnum::CommunicationNotEstablished;
                        QByteArray deslect_rsp = MessageUtil::DeSelect_Rsp(sessionId, transactionId, deselect_status);
                        emit SendMsg(tr("接收到来自主机的DeSelectReq,但是目前设备已经处于NOT_SELECTED状态"));
                        emit SendMsg(tr("设备回复了DeSelectRsp,但是不会发生任何变化"));
                        d->socket->write(deslect_rsp);
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的DeSelectReq,且设备处于SELECTED状态"));
                        emit SendMsg(tr("设备回复了DeSelectRsp,且设备的ConnectionState从SELECTED-->NOT_SELECTED"));
                        quint16 sessionId = ParseUtil::Get_SessionId(message);
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        SecsEnum::DeselectStatus deselect_status = SecsEnum::CommunicationEnded;
                        QByteArray deslect_rsp = MessageUtil::DeSelect_Rsp(sessionId, transactionId, deselect_status);
                        d->socket->write(deslect_rsp);

                        d->currentConnectionState = SecsEnum::NOT_SELECTED;
                        emit ConnectionStateChange(SecsEnum::NOT_SELECTED);
                        return;
                    }
                }

                /*
                 * 第六种情况:接收到来自主机Host的DeSelect回应
                 * 说明在此之前,设备端自己主动尝试向主机发送DeSelect请求
                 */
                if(ParseUtil::Is_DeselectRsp(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接没有建立,这个消息你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的DeSelectRsp,且当前设备处于NOT_SELECTED"));
                        emit SendMsg(tr("设备之前发送的DeSelectReq成功响应,但是不会发生任何变化"));
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的DeSelectRsp,且当前设备处于SELECTED"));
                        emit SendMsg(tr("设备之前发送的DeSelectReq成功响应,ConnectionState从SELECTED-->NOT_SELECTED"));

                        d->currentConnectionState = SecsEnum::NOT_SELECTED;
                        emit ConnectionStateChange(SecsEnum::NOT_SELECTED);
                        return;
                    }
                }

                /*
                 * 第七种情况:收到来自主机的心跳检测的请求
                 * 我们需要及时回复LinkTest.Rsp来维持SECS通信的稳定性
                 */
                if(ParseUtil::Is_LinkTestReq(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("TCP连接没有建立,这个心跳你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("SelectProcedure流程没有进行,这个心跳你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的心跳检测"));
                        quint32 transactionId = ParseUtil::Get_TransactionId(message);
                        QByteArray linktestrsp = MessageUtil::Link_Test_Rsp(transactionId);
                        d->socket->write(linktestrsp);
                        emit SendMsg(tr("成功向主机发送心跳检测的响应"));
                    }
                }

                /*
                 * 第八种情况:我们主动向Host发出了心跳检测的请求后,收到来自主机的回应
                 */
                if(ParseUtil::Is_LinkTestRsp(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {
                        emit SendMsg(tr("SelectProcedure流程没有进行,这个心跳你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {
                        emit SendMsg(tr("SelectProcedure流程没有进行,这个心跳你从哪里接收到的?"));
                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {
                        emit SendMsg(tr("接收到来自主机的心跳检测回应"));
                    }
                }

                /*
                 * RejectReq和SeparateReq的情况先暂时不给予讨论
                 */
                if(ParseUtil::Is_RejectReq(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {

                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {

                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {

                    }
                }

                if(ParseUtil::Is_SeparateReq(message))
                {
                    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
                    {

                    }

                    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
                    {

                    }

                    if(d->currentConnectionState == SecsEnum::SELECTED)
                    {

                    }
                }
            }
        });
    });
}

void PassiveEquipment::BindPort(int port)
{

    bool result = d->server->listen(QHostAddress::Any, port);

    if(result)
    {
        emit SendMsg(tr("当前设备已经绑定端口%1,等待远程主机的连接").arg(port));
    }

    else
    {
        emit SendMsg(tr("当前设备绑定端口失败"));
    }
}

/*
 * PrimaryMessage的发送主要分为以下的几种情况
 * condition1:二者没有建立TCP连接,什么都发送不了
 * condition2:没有进行SelectProcedure,什么都发送不了
 * condition3:处于SELECTED但是处于NOT_COMMUNICATING,若发送的不是S1F13,则不进行发送
 * condition4:处于SELECTED但是处于NOT_COMMUNICATING,若发送的是S1F13,则进行发送
 * condition5:处于COMMUNICATING,可以进行任意消息的发送
 */
void PassiveEquipment::SendPrimaryMessage(quint16 sessionId,
                                          quint32 transactionId,
                                          quint8 stream,
                                          quint8 function)
{
    QByteArray message = MessageUtil::SxFy(sessionId, transactionId, stream, function, true, d->message);

    if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
    {
        emit SendMsg(tr("当前设备没有和任何远程主机建立TCP连接,"
                        "请建立TCP连接并完成SelectProcedure后在进行消息的发送"));
    }

    if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
    {
        emit SendMsg(tr("当前设备虽然和远程主机建立了TCP连接,"
                        "但是并没有完成SelectProcedure,请进行Select.Req的发送"));
    }

    if(d->currentConnectionState == SecsEnum::SELECTED)
    {
        if(d->currentCommunicationState == SecsEnum::NOT_COMMUNICATING && !Is_S1F13(message))
        {
            emit SendMsg(tr("当前设备需要和远程主机建立了通信链路,请发送S1F13以完成此过程"));
        }

        if(d->currentCommunicationState == SecsEnum::NOT_COMMUNICATING && Is_S1F13(message))
        {
            d->socket->write(message);
            emit SendMsg(tr("设备向主机发送了消息S1F13,方向:Equipment-->Host"));
        }

        if(d->currentCommunicationState == SecsEnum::COMMUNICATING)
        {
            d->socket->write(message);
            int stream = ParseUtil::Get_Stream(message);
            int function = ParseUtil::Get_Function(message);
            emit SendMsg(tr("设备向主机发送了消息S%1F%2,方向:Equipment-->Host").arg(stream).arg(function));
        }
    }
}

void PassiveEquipment::SendControlMessage(QByteArray message)
{
    /*
     * 第一种情况:设备主动发送Select请求
     * condition1:设备处于NOT_CONNECTED,无法发送该消息
     * condition2:设备处于NOT_SELECTED,可以发送该消息
     * condition3:设备处于SELECTED,可以发送该消息,但是瞎折腾
     */
    if(ParseUtil::Is_SelectReq(message))
    {
        if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
        {
            emit SendMsg(tr("请先建立TCP的连接在做打算"));
        }

        if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
        {
            d->socket->write(message);
            emit SendMsg(tr("设备端向远程主机发送了Select.Req"));
        }

        if(d->currentConnectionState == SecsEnum::SELECTED)
        {
            emit SendMsg(tr("你已经和远程主机完成了SelectProcedure,该条消息无需发送"));
        }
    }

    /*
     * 第二种情况:设备主动发送DeSelect请求
     * condition1:设备处于NOT_CONNECTED,无法发送该消息
     * condition2:设备处于NOT_SELECTED,可以发送该消息,但是瞎折腾
     * condition3:设备处于SELECTED,可以发送该消息
     */
    if(ParseUtil::Is_DeselectReq(message))
    {
        if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
        {
            emit SendMsg(tr("请先建立TCP的连接在做打算"));
        }

        if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
        {
            emit SendMsg(tr("你已经和远程主机断开了SECS的连接,该条消息无需发送"));
        }

        if(d->currentConnectionState == SecsEnum::SELECTED)
        {
            d->socket->write(message);
            emit SendMsg(tr("设备端向远程主机发送了DeSelect.Req"));
        }
    }

    /*
     * 第三种情况:设备主动发送心跳检测的请求
     * condition1:设备处于NOT_CONNECTED,无法发送该消息
     * condition2:设备处于NOT_SELECTED,无法发送该消息
     * condition3:设备处于SELECTED,可以发送该消息
     */
    if(ParseUtil::Is_LinkTestReq(message))
    {
        if(d->currentConnectionState == SecsEnum::NOT_CONNECTED)
        {
            emit SendMsg(tr("请先建立TCP的连接在做打算"));
        }

        if(d->currentConnectionState == SecsEnum::NOT_SELECTED)
        {
            emit SendMsg(tr("你已经和远程主机断开了SECS的连接,无法进行心跳的检测"));
        }

        if(d->currentConnectionState == SecsEnum::SELECTED)
        {
            d->socket->write(message);
            emit SendMsg(tr("设备端向远程主机发送了LinkTest.Req"));
        }
    }

    /*
     * 针对设备端是否可以进行这两种ControlMessage的发送,在此没有进行讨论,后续会进行补充
     */
    if(ParseUtil::Is_RejectReq(message))
    {

    }

    if(ParseUtil::Is_SeparateReq(message))
    {

    }
}

bool PassiveEquipment::Is_S1F13(QByteArray message)
{
    int stream = ParseUtil::Get_Stream(message);
    int function = ParseUtil::Get_Function(message);

    if(stream == 1 && function == 13)
    {
        return true;
    }

    return false;
}

bool PassiveEquipment::Is_S1F14(QByteArray message)
{
    int stream = ParseUtil::Get_Stream(message);
    int function = ParseUtil::Get_Function(message);

    if(stream == 1 && function == 14)
    {
        return true;
    }

    return false;
}

void PassiveEquipment::DisConnect()
{
    d->server->close();
    d->socket->disconnectFromHost();
    emit SendMsg(tr("设备端停止了运行,请重新绑定端口"));
}
